<?php

namespace app\speedDating\model\traits;

use think\Model;

/**
 * @property string[] $createTimeField 创建时间字段
 * @property string[] $updateTimeField 更新时间字段
 * @property string[] $SimpleMainField 查询的字段数组
 * @property string[] $selectResultDateTimeFieldParse 需要进行时间解析的字段
 */
trait SimpleModel
{
    private static $findResultDate; //find结果
    private static $selectResultDate; //select结果

    //insert

    /**
     * 统一插入数据
     * @param $ins
     * @return mixed
     */
    public static function toInsert($ins)
    {
        if (!empty(self::$createTimeField ?? null)) {
            $ins[self::$createTimeField] = time();
        }

        return self::create($ins);
    }

    //update

    /**
     * 统一过滤,再修改
     * @param $ins
     * @param $where
     * @return mixed | void
     */
    public static function toUpdate($ins, $where)
    {
        $ins = array_filter($ins);
        if (!empty($ins) && !empty($where)) {
            if (!empty(self::$updateTimeField ?? null)) {
                $ins[self::$updateTimeField] = time();
            }
            return self::update($ins, $where);
        }
    }

    //select

    /**
     * 返回包含所有列的单行数据
     * @param $id
     * @return SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCompleteByID($id)
    {
        $result = self::find($id);
        return self::parseFindResult($result);
    }

    /**
     * 返回包含所有列的数据列表
     * @param $size
     * @param $page
     * @param $where \Closure|array|null
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCompleteList($size, $page, $where = [])
    {
        $result = self::where($where)->order('id', 'desc')->limit($size)->page($page)->select();
        return self::parseSelectResult($result);
    }

    /**
     * 返回包含所有列的所有数据
     * @param $where \Closure|array|null
     * @return SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCompleteAll($where = [])
    {
        $result = self::where($where)->order('id', 'desc')->select();
        return self::parseSelectResult($result);
    }


    /**
     * 返回包含指定列的单行数据
     * @param $id
     * @param $lock
     * @return SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getSimpleMainByID($id, $lock = false)
    {
        $result = self::field(self::$SimpleMainField)->lock($lock)->find($id);
        return self::parseFindResult($result);
    }

    /**
     * 返回包含指定列的数据列表
     * @param $size
     * @param $page
     * @param $where \Closure|array|null
     * @param $order array
     * @return SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getSimpleMainList($size, $page, $where = [], $order = [])
    {
        $result = self::field(self::$SimpleMainField)->where($where)->order('id', 'desc')->limit($size)->page($page)->order($order[0] ?? 'id', $order[1] ?? 'desc')->select();
        return self::parseSelectResult($result);
    }

    /**
     * 返回包含指定列的所有数据
     * @param $where \Closure|array|null
     * @param $order array
     * @return SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getSimpleMainAll($where = [], $order = [])
    {
        $result = self::field(self::$SimpleMainField)->where($where)->order($order[0] ?? 'id', $order[1] ?? 'desc')->select();
        return self::parseSelectResult($result);
    }

    /**
     * @overwrite
     * @param $data
     * @return SimpleModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function parseSelectResult($data = null)
    {
        self::$selectResultDate = $data;
        self::dateListTimeFieldParse();

        return self::$selectResultDate;
    }

    /**
     * 对响应结果列表进行时间转换
     * @return void
     */
    private static function dateListTimeFieldParse()
    {
        if (!empty(self::$selectResultDate) && !self::$selectResultDate->isEmpty() && !empty(self::$selectResultDateTimeFieldParse)) {
            foreach (self::$selectResultDate as $item) {
                foreach (self::$selectResultDateTimeFieldParse as $v) {
                    if (!empty($item->$v)) $item->$v = date('Y-m-d H:i:s', $item->$v);
                }
            }
        }
        return self::$selectResultDate;
    }

    /**
     * @overwrite
     * @param $data
     * @return SimpleModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function parseFindResult($data = null)
    {
        self::$findResultDate = $data;
        self::dateTimeFieldParse();

        return self::$findResultDate;
    }

    /**
     * 对响应结果进行时间转换
     * @return void
     */
    private static function dateTimeFieldParse()
    {
        if (!empty(self::$findResultDate) && !self::$findResultDate->isEmpty() && !empty(self::$selectResultDateTimeFieldParse)) {
            foreach (self::$selectResultDateTimeFieldParse as $v) {
                if (!empty(self::$findResultDate->$v)) self::$findResultDate->$v = date('Y-m-d H:i:s', self::$findResultDate->$v);
            }
        }
    }


}